﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Chinaer.DTO;

namespace Chinaer.Web
{
    public partial class DingDan : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindDingDanStatus();
                Binding();
            }
        }
        public void BindDingDanStatus()
        {
            List<DTO.DingDanStatusDTO> listDingDanStatus = CommonService.GetChinaerService.GetAllDingDanStatus("");
            this.ddlDingDanStatus.DataSource = listDingDanStatus;
            this.ddlDingDanStatus.DataTextField = "Name";
            this.ddlDingDanStatus.DataValueField = "ID";
            this.ddlDingDanStatus.DataBind();

            List<DTO.AddressDTO> listAddress = CommonService.GetChinaerService.GetAddressByCustomerID(this.txtCustomerID.Text.Trim(), "");
            this.ddlAddress.DataSource = listAddress;
            this.ddlAddress.DataTextField = "Address";
            this.ddlAddress.DataValueField = "ID";
            this.ddlAddress.DataBind();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Session["apiToken"] == null)
            {
                Response.Redirect("login.aspx");
            }
            string strID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID();

            DTO.DingDanDTO countryDTO = new DingDanDTO()
            {
                ApiToken = Session["apiToken"].ToString(),
                AddressID = this.ddlDingDanStatus.SelectedValue,
                CustomerID = this.txtCustomerID.Text.Trim(),
                DianPuID = this.txtDianPuID.Text.Trim(),
                DingDanStatusID = this.ddlDingDanStatus.SelectedValue,
                ModifyTime = DateTime.Now,
                Name = this.txtDingDanName.Text.Trim(),
                SubPrice = decimal.Parse(this.txtDingDanPrice.Text.Trim()),
                CreateTime = DateTime.Now,
                IsDelete = false,
                LoginStatus = Core.BaseCore.LoginStatus.Success,
                IsUse = true,
                ID = strID
            };
            var result = CommonService.GetChinaerService.AddDingDan(countryDTO);
            if (result.Success == true)
            {
                this.txtID.Text = strID;
            }
            Response.Write(result);
            Binding();


        }
        private void Binding()
        {
            List<DTO.DingDanDTO> listDingDan = CommonService.GetChinaerService.GetDingDanByTime(DateTime.Now.AddDays(-10).ToString(), DateTime.Now.AddDays(10).ToString(), this.txtDianPuID.Text.Trim(), Session["apiToken"].ToString());
            if (listDingDan != null)
            {
                this.gvList.DataSource = listDingDan;
                this.gvList.DataBind();
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (Session["apiToken"] == null)
            {
                Response.Redirect("login.aspx");
            }
            DTO.DingDanDTO countryDTO = new DingDanDTO()
            {
                ApiToken = Session["apiToken"].ToString(),
                AddressID = this.ddlDingDanStatus.SelectedValue,
                CustomerID = this.txtCustomerID.Text.Trim(),
                DianPuID = this.txtDianPuID.Text.Trim(),
                DingDanStatusID = this.ddlDingDanStatus.SelectedValue,
                ModifyTime = DateTime.Now,
                Name = this.txtDingDanName.Text.Trim(),
                SubPrice = decimal.Parse(this.txtDingDanPrice.Text.Trim()),
                CreateTime = DateTime.Now,
                IsDelete = false,
                LoginStatus = Core.BaseCore.LoginStatus.Success,
                IsUse = true,
                ID = this.txtID.Text.Trim()
            };
            var result = CommonService.GetChinaerService.UpdateDingDan(countryDTO);
            Response.Write(result);
            Binding();
        }

        protected void gvList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void gvList_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string strID = this.txtID.Text.Trim();
            var result = CommonService.GetChinaerService.DeleteDingDanByID(strID);
            if (result.Success == true)
            {
                Response.Write("删除成功");
                Binding();
            }
            else
            {
                Response.Write("删除失败");
            }
        }
    }
}